import datetime
from django.db import models
from django import forms
from django.forms import Textarea, DateTimeInput
#from tinymce.widgets import TinyMCE

# Create your models here.

class NoteCategory(models.Model):
    name = models.CharField(max_length=20, primary_key=True)

TYPE_CHOICES = (
    ('MR', 'Mr.'),
    ('MRS', 'Mrs.'),
    ('MS', 'Ms.'),
)

class Note(models.Model):
    #ForeignKey, ManyToManyField and OneToOneField require the first argument to
    #be a model class, so use the verbose_name keyword argument:
    notecategory = models.ForeignKey(NoteCategory, verbose_name="Category",
                                     to_field='name')

    # Each field type, except for ForeignKey, ManyToManyField and OneToOneField,
    # takes an optional first positional argument -- a verbose name.
    #categaory = models.ChoiceFiled()
    title = models.CharField(max_length=64, verbose_name='Title', blank=True)

    content = models.TextField(max_length=2048)
    timestamp = models.DateTimeField(verbose_name='Created', auto_now=True)

    def __unicode__(self):
        return self.content

    def was_published_today(self):
        return self.timestamp.date() == datetime.date.today()

class NoteForm(forms.ModelForm):
    #error_css_class = 'error'
    #required_css_class = 'required'
    class Meta:
        model = Note
        # !!! curr_date auto_now = True, can't be listed here !!!
        fields = ('notecategory', 'title', 'content')
        widgets = {
            #'notecategory' :  Select(),
            'content' : Textarea(attrs={'cols' : 80, 'rows' : 10 }),
            #'content' : TinyMCE(attrs={'cols': 80, 'rows': 20})
            #'content' : TinyMCE()
        }

TITLE_CHOICES = (
    ('MR', 'Mr.'),
    ('MRS', 'Mrs.'),
    ('MS', 'Ms.'),
)

class Author(models.Model):
    name = models.CharField(max_length=100)
    title = models.CharField(max_length=3, choices=TITLE_CHOICES)
    birth_date = models.DateField(blank=True, null=True)

    def __unicdoe__(self):
        return self.name

class Book(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)

class AuthorForm(forms.ModelForm):
    class Meta:
        model = Author

class PartialAuthorForm(forms.ModelForm):
    '''Using a subset of fields on the form
    '''
    # Overide a field's default label
    birth_date = forms.DateField(label='Birthday')

    class Meta:
        model = Author
        fields = ('name', 'title')
        # Override the default field types ow widgets
        widgets = {
            'name' : Textarea(attrs={'cols' : 80, 'rows' : 20 }),
        }

class BookForm(forms.ModelForm):
    class Meta:
        model = Book

'''
# Create a form instance from POST data.
>>> f = ArticleForm(request.POST)

# Save a new Article object from the form's data.
>>> new_article = f.save()

# Create a form to edit an existing Article.
>>> a = Article.objects.get(pk=1)
>>> f = ArticleForm(instance=a)
>>> f.save()

# Create a form to edit an existing Article, but use
# POST data to populate the form.
>>> a = Article.objects.get(pk=1)
>>> f = ArticleForm(request.POST, instance=a)
>>> f.save()
'''

"""
article = Article.objects.get(pk=1)
form = ArticleForm(instance=article)

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
        return self.question

    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()

    def __unicode__(self):
        return self.choice

class Category(models.Model):
    name = models.CharField(core=True, maxlength=200)
    slug = models.SlugField(prepopulate_from=('name',))
    parent = models.ForeignKey('self', blank=True, null=True, related_name='child')
    description = models.TextField(blank=True,help_text="Optional")
            
    class Admin:
        list_display = ('name', '_parents_repr')
                
    def __unicode__(self):
        p_list = self._recurse_for_parents(self)
        p_list.append(self.name)
        return
    self.get_separator().join(p_list)
            
    def get_absolute_url(self):
        if self.parent_id:
            return "/tag/%s/%s/" % (self.parent.slug, self.slug)
        else:
            return "/tag/%s/" % (self.slug)
                
    def _recurse_for_parents(self, cat_obj):
        p_list = []
        if cat_obj.parent_id:
            p = cat_obj.parent
            p_list.append(p.name)
            more = self._recurse_for_parents(p)
            p_list.extend(more)
        if cat_obj == self and p_list:
            p_list.reverse()
        return p_list
                        
    def get_separator(self):
        return ' :: '
                        
    def _parents_repr(self):
        p_list = self._recurse_for_parents(self)
        return self.get_separator().join(p_list)
    _parents_repr.short_description = "Tag parents"
                    
    def save(self):
        p_list = self._recurse_for_parents(self)
        if self.name in p_list:
            raise validators.ValidationError("You must not save a category in itself!")
        super(Category, self).save()
"""

